/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package EDT.api;

import EDT.obj.Administrateur;
import EDT.obj.Calendrier;
import EDT.obj.Creneau;
import EDT.obj.Ec;
import EDT.obj.Formation;
import EDT.obj.FormationConsult;
import EDT.obj.IntervUsers;
import EDT.obj.Intervenant;
import EDT.obj.PlaquetteForm;
import EDT.obj.PlaquetteInterv;
import EDT.obj.Salle;
import EDT.obj.Session;
import EDT.obj.Status;

import EDT.obj.StratEc;
import EDT.obj.StratEcInterv;
import EDT.obj.StratInterv;
import EDT.obj.StratPromo;

import EDT.obj.Ue;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;
import java.util.TreeMap;


/**
 *
 * @author ali
 */
public class API {
    
    
    private static char hexTab[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    private static int lg = Integer.MAX_VALUE;
    
    
    public static Calendrier getCalendrierPromo(int idp, String periode,int nbrPeriode,int nbGroupe){
        Calendrier c;
        
        if (periode.equals("all")) {
            c= new  Calendrier(new StratEc(),nbrPeriode,0);
        }
        else {
            c = new  Calendrier(new StratEc(),nbrPeriode,Integer.parseInt(periode));
        } 
        for (Creneau cr : EDT.api.BDUser.getlesCreneau(idp, periode,nbGroupe)) {
            c.addCreneau(cr);
        }
        c.setGuidePeriode(EDT.api.BDUser.getGuidePeriode(idp));
        return c;
    }
    
    public static Calendrier getAllCalendrierPromo(String key, int idp, String periode,int nbrPeriode,int nbGroupe){
        
        
        Calendrier c ;
        
     if (isKeyValide(key)){
        if (periode.equals("all")) {
             c= new  Calendrier(new StratEc(),nbrPeriode, 0);
         }
        else {
             c = new  Calendrier(new StratEc(),nbrPeriode,Integer.parseInt(periode));
         } 
        for (Creneau cr : EDT.api.BDUser.getTouslesCreneau(idp, periode,nbGroupe)) {
             c.addCreneau(cr);
         }
        c.setGuidePeriode(EDT.api.BDUser.getGuidePeriode(idp));
        return c;
     }else {
            return new Calendrier(new StratEc());
        }
        
    }
    
    public static String getAnneeMax(String key){
        
        return EDT.api.BDUser.getAnneeMax();
    }
    
    public static Calendrier getCalendrierInterv(int idi, int annee){
        
        Calendrier c = new  Calendrier(new StratEcInterv());
        
        for (Creneau cr : EDT.api.BDUser.getCreneauInterv(idi, annee)) {
            c.addCreneau(cr);
        }
        
        //c.setGuidePeriode(EDT.bdd.BDUser.getGuidePeriode(idp));
        return c;
    }
    
    public static Calendrier getCalendrierEc(int idec, int idp){
        
        Calendrier c = new  Calendrier(new StratInterv());
        
        for (Creneau cr : EDT.api.BDUser.getCreneauEc(idec, idp)) {
            c.addCreneau(cr);
        }
        
       
         int a = EDT.api.BDUser.getAnnee(idp);
        
        //c.setTitre("<br/> EC "+c.getFirstCreneau().getEc()+"<br/> promotion "+a+"/"+(a+1));
        //c.setGuidePeriode(EDT.bdd.BDUser.getGuidePeriode(idp));
        return c;
    }
    
    public static int getAnnee(int idp){
        return EDT.api.BDUser.getAnnee(idp);
    }
    public static ArrayList<Salle> getSalle(){
        return EDT.api.BDUser.getSalle();
    }
    
    public static ArrayList<Integer> getAnneeMinMax(){
            
            return EDT.api.BDUser.getAnneeMinMax();
    }

    public static Calendrier getCalendrierSalle(int ids, int annee) {
        
        Calendrier c = new  Calendrier(new StratPromo());
        
        for (Creneau cr : EDT.api.BDUser.getCreneauSalle(ids, annee)) {
            c.addCreneau(cr);
        }
        return c;
    }
    
    public static String ajouterFormation(String key,String grade,String niveau,String filiere,String parcours,String type ,int anneeex,int periode){
        
        if (isKeyValide(key)){
 
        return EDT.api.BDUser.ajouterFormation(grade.toUpperCase(),niveau,filiere.toUpperCase(),parcours.toUpperCase(),type.toUpperCase() ,anneeex,periode);
        }else {
            return "key not valide";
        }

    }

    public static boolean isKeyValide(String key){
        
        if(key == null){
            return false;
        }else{
            return BDUser.isKeyValid(key);
        }
    
    }
    
    public static boolean isKeyValideInterv(String key, Integer interv) {
        
        if(key == null || interv == null){
            return false;
        }else{
        return BDUser.isKeyValidInterv(key,interv);
        }
        
   }
    

    public static String ajouterUe(String key, int idFormation, String nomUE, int nperiode) {
       //if (isKeyValide(key)){
            return EDT.api.BDUser.ajouterUe(idFormation,nomUE.toUpperCase(),nperiode);
       /* }else {
            return "key not valide";
        }*/
    }

    public static String ajouterEc(String key, String nom, String code) {
        //if (isKeyValide(key)){
            return EDT.api.BDUser.ajouterEc(nom.toUpperCase(),code);
       /* }else {
            return "key not valide";
        }*/
    }
    
    public static ArrayList<Ue> getUe(String key,int idf){
        
        if (isKeyValide(key)){
            return EDT.api.BDUser.getUe(idf);
        }else {
            return new ArrayList<>();
        }
        
    }
    
    public static ArrayList<Ec> getEc(String key){
        
        if (isKeyValide(key)){

        return EDT.api.BDUser.getEc();
        }else {
            return new ArrayList<>();
        }
  
    }

    
    public static String AffectECUE(String key, int formation, int ec, int ue,int nbrcm,int nbrtd, double coeff) {
    if (isKeyValide(key)){

        return EDT.api.BDUser.AffectECUE(formation,ec,ue,nbrcm,nbrtd,coeff);
        }else {
            return "key not valide";
        }
    }

    
    public static String ajouterPromotion(String key, int formation, int annee, int nbrGp, String[] periodeStart, String[] periodeend) {
    if (isKeyValide(key)){
        String m="";
        
        m += EDT.api.BDUser.AjoutPromotion(formation,annee,nbrGp);
        
        if (m.equals("") ){
            
            m+= EDT.api.BDUser.AjoutDatePeriodes(formation,annee,periodeStart,periodeend);  
            
            
            m += EDT.api.BDUser.AjoutCalendrier(annee);  
            
            
            m += BDUser.AjouterFicheVoeuxParDefault(annee);
            
                
        }
        return m;
 
    }else {
            return "key not valide";
        }
    
    }

    public static String ajouterSalle(String key, String num, String nom, String batiment, String type) {
         if (isKeyValide(key)){
         
            
        return EDT.api.BDUser.ajouterSalle(num,nom,batiment,type);
        }else {
            return "key not valide";
        }
    }

    public static String AjouterStatus(String key, String nom, String nbrTD, String nbrSup) {
        if (isKeyValide(key)){
         
            
        return EDT.api.BDUser.ajouterStatus(nom,nbrTD,nbrSup);
        }else {
            return "key not valide";
        }
    }
    
    
    
    public static ArrayList<Status> getStatus(String key){
        if (isKeyValide(key)){
        
        return EDT.api.BDUser.getStatus();
        }else {
            return new ArrayList<>();
        }
    }

    public static String ajouterIntervanant(String key, String nom, String prenom, String mail, String tel, String pass, int status) {
        if (isKeyValide(key)){
         
            
        return EDT.api.BDUser.ajouterIntervenant(nom, prenom, mail, tel, pass, status);
        }else {
            return "key not valide";
        }
    }
    
    
     public static ArrayList<Intervenant> getIntervenantEtat(String key){
        if (isKeyValide(key)){
        
        return EDT.api.BDUser.getIntervenantEtat();
        }else {
             return new ArrayList<>();
         }
    }
    
    public static ArrayList<Intervenant> getAllIntervenant(){
        //if (isKeyValide(key)){
        
        return EDT.api.BDUser.getAllIntervenant();
        //}else
          //  return new ArrayList<>();
    }

    public static String AffectEcIterv(String key, int ec, int interv) {
        if (isKeyValide(key)){
         
            
        return EDT.api.BDUser.AffectEcIterv(ec,interv);
        }else {
            return "key not valide";
        }
    }

    public static String ajouterFicheVoeux(String key, int interv, int annee, String periode, int moment1, int poids1, int moment2, int poids2, int moment3, int poids3, int moment4, int poids4, int moment5, int poids5,String requette,String valide) {
        
        //V��rifier si la cl�� appartient �� l'intervenant !
        if (isKeyValideInterv(key,interv)){
         
            ArrayList<Integer> moment = new ArrayList<>();
            ArrayList<Integer> poids = new ArrayList<>();
            moment.add(moment1);
            poids.add(poids1);
            moment.add(moment2);
            poids.add(poids2);
            moment.add(moment3);
            poids.add(poids3);
            moment.add(moment4);
            poids.add(poids4);
            moment.add(moment5);
            poids.add(poids5);
        
            if (valide == null) {
                valide="fals";
            }
            else {
                valide="true";
            }
            
        if(requette.equals("ajout")) {
                return EDT.api.BDUser.ajouterFicheVoeux(interv, annee, periode, moment,poids,valide,new ArrayList<Integer>());
            }
        else {
                return EDT.api.BDUser.modifierFicheVoeux(interv, annee, periode, moment,poids,valide,new ArrayList<Integer>());
            }
        }
        else {
            return "key not valide";
        }
    }
    
    
    public static String modifierFicheVoeux(String key, int interv, int annee,
            String periode, int moment1, int poids1,int priorit1, 
            int moment2, int poids2,int priorit2, 
            int moment3, int poids3,int priorit3, 
            int moment4, int poids4,int priorit4,
            int moment5, int poids5,int priorit5,
            String requette,String valide) {
        
        //V��rifier si la cl�� appartient �� l'intervenant !
        if (isKeyValide(key)){
         
            ArrayList<Integer> moment = new ArrayList<>();
            ArrayList<Integer> poids = new ArrayList<>();
            ArrayList<Integer> priorite = new ArrayList<>();
            
            moment.add(moment1);
            poids.add(poids1);
            priorite.add(priorit1);
            
            moment.add(moment2);
            poids.add(poids2);
            priorite.add(priorit2);
            
            moment.add(moment3);
            poids.add(poids3);
            priorite.add(priorit3);
            
            moment.add(moment4);
            poids.add(poids4);
            priorite.add(priorit4);
            
            moment.add(moment5);
            poids.add(poids5);
            priorite.add(priorit5);
        
            if (valide == null) {
                valide="fals";
            }
            else {
                valide="true";
            }
            
        if(requette.equals("ajout")) {
                return EDT.api.BDUser.ajouterFicheVoeux(interv, annee, periode, moment,poids,valide,priorite);
            }
        else {
                return EDT.api.BDUser.modifierFicheVoeux(interv, annee, periode, moment,poids,valide,priorite);
            }
        }
        else {
                    return "key not valide";
                }
    }
    
    public static ArrayList<Intervenant> getIntervenant(String key,Integer idec){
         if (isKeyValide(key)){
             
             return EDT.api.BDUser.getIntervenant(idec);
         }
        else {
            return new ArrayList<>();
        }
        
    }
    
    public static ArrayList<Intervenant> getIntervDiso(String key,Integer idec,String jour, int debut, int fin,int promo){
         if (isKeyValide(key)){
             
             return EDT.api.BDUser.getIntervDispo(idec,jour,debut,fin,promo);
         }
        else {
            return new ArrayList<>();
        }
        
    }
    
    
    
    public static ArrayList<Salle> getSalleDispo(String key,String type,String jour, int debut, int fin){
         if (isKeyValide(key)){
             
             return EDT.api.BDUser.getSalleDispo(type,jour,debut,fin);
         }
        else {
            return new ArrayList<>();
        }
        
    }

    public static String AjouterCreneau(String key, int salle, int idp, int ec, int interv, String jour, int gp, String type, int debut, int fin,int nbrCr,String visible) {
       if (isKeyValide(key)){
         
        if(nbrCr == 1) {
               return    BDUser.ajouterUnCreneau(null,salle,idp,ec,interv,jour,gp,type,debut,fin,"true",visible);
           }
        else {
        TreeMap<Integer,String> lesJours = BDUser.getJoursFrom(jour,nbrCr);   
            
            
            return BDUser.ajouterDesCreneaux(salle,idp,ec,interv,lesJours,gp,type,debut,fin,visible);
        }
        
       }else {
            return "key not valide";
        }
    
    }
    
    public static ArrayList<Formation> getFormation(){
        return BDUser.getFormation();
    }
    
    public static ArrayList<Ec> getEC(int idf,int periode){
        return BDUser.getEC(idf, periode);
    }
    
    public static int getNbrGp(int idp) {
        return BDUser.getNbrGp(idp);
    }
      
    public static ArrayList<Ec> getEC(int idf){
        return BDUser.getEC(idf);
    }
    
    public static ArrayList<Intervenant> getIntervenant(){
        return BDUser.getIntervenant();
    }
    
    public static Date getJourMax(){
        return BDUser.getJourMax();
    }

    public static String ajouterJourFerie(String key, String date,String option) {
        if (isKeyValide(key)){
         
            
        return EDT.api.BDUser.ajouterJourFerie(date,option);
        }
        else {
            return "key not valide";
        }
    }

    public static String SupprimerCr(String key, String[] options) {
        if (isKeyValide(key)){
         
            
        return EDT.api.BDUser.SupprimerCr(options);
        }
        else {
            return "key not valide";
        }
    }
    
    public static String getJourferie(int annee){
        
        return BDUser.getJourferie(annee);
    }
    
    public static String getJourferie(int debut,int fin){
        
        return BDUser.getJourferie(debut,fin);
    }
    
    public static String getFormModificationFicheVoeux(String key,int interv,int annne,String periode){
        if (isKeyValideInterv(key,interv)){
         
            
        return EDT.api.BDUser.getFormModifFichVoeux(interv,annne,periode,new IntervUsers());
        }
        else {
            return "key not valide";
        }
        
    }
    
    
    public static String getFormModificationFicheVoeuxAdmin(String key,int interv,int annne,String periode){
        if (isKeyValide(key)){
         
            
        return EDT.api.BDUser.getFormModifFichVoeux(interv,annne,periode, new Administrateur());
        }
        else {
            return "key not valide";
        }
        
    }

    public static String verifPlaqueHPromo(String key, int promotion, int groupe, int nbrperiode, String formation) {
        if (isKeyValide(key)){
        String ret="";
        
        int annee = BDUser.getAnnee(promotion);
        ret +="<H3>Plaquette de formation "
                +formation+" <br/>";
        ret += "promotion "+annee+"/"+(annee+1)+"<br/>"
                +"Groupe "+groupe +"</H3>";
        
        Double nbrHTotalfait = 0.0;
        Double nbrHTotalObligatoire = 0.0;
        
        Double nbrHTotalfaitTmp ;
        Double nbrHTotalObligatoireTmp ;
        
        for (int i=1;i<=nbrperiode;i++){
        ret += "<table class='calendrier calendrier-etroit'> <caption> <span>Periode : "+i+" </span></caption>";
        
        ret += "<tr><th>UE</th><th>EC</th><th>Nbr H CM</th><th>Nbr H TD TP || (TD+TP)</th><th> Nbr H (CM+TD+TP) </th><th> Nbr S��ance CM </th><th> Nbr S��ance TD </th><th> Nbr S��ance TP </th></tr>";            
        
        TreeMap<String, PlaquetteForm> map = BDUser.getPlaquette(promotion,groupe);
                for (String cle : map.keySet()){
                    
                    if(map.get(cle).getNperiode()==i){
                        
                        
                    nbrHTotalfaitTmp = map.get(cle).getNbrHCM()
                                +map.get(cle).getNbrHTD()
                                +map.get(cle).getNbrHTME();
                    
                    
                    nbrHTotalObligatoireTmp = map.get(cle).getNbrHCMTotal()
                                             +map.get(cle).getNbrHTDTMETotal();
                        
                    nbrHTotalfait += nbrHTotalfaitTmp;
                    nbrHTotalObligatoire += nbrHTotalObligatoireTmp;
                    
                    ret += "<tr><td>"+map.get(cle).getUeNom()+"</td>"
                            + "<td>"+map.get(cle).getEcNom()+"</td>"
                            + "<td>"
                            
                                +getCouleur(map.get(cle).getNbrHCM(),map.get(cle).getNbrHCMTotal())
                                +map.get(cle).getNbrHCM()+"/"
                                +map.get(cle).getNbrHCMTotal()+"</label></td>"
                                
                                        
                            + "<td>"
                            
                                +getCouleur((map.get(cle).getNbrHTD()
                                        +map.get(cle).getNbrHTME()),
                                            map.get(cle).getNbrHTDTMETotal())
                                + "TD:"+map.get(cle).getNbrHTD()
                                +" TP:"+map.get(cle).getNbrHTME()
                                +" || "+(map.get(cle).getNbrHTD()
                                        +map.get(cle).getNbrHTME())
                                +"/"+map.get(cle).getNbrHTDTMETotal()+"</label></td>"
                                
                                +"<td>"+getCouleur(nbrHTotalfaitTmp,
                                    nbrHTotalObligatoireTmp)
                                +nbrHTotalfaitTmp+"/"+nbrHTotalObligatoireTmp
                                +"</label></td>"
                                
                                + "<td>"+map.get(cle).getNbrSeanceCM()+"</td>"
                                + "<td>"+map.get(cle).getNbrSeanceTD()+"</td>"
                                + "<td>"+map.get(cle).getNbrSeanceTME()+"</td>"
                            
                            
                            +"</tr>";
                    
                    
                    }
                }
        
        ret += "</table><br/>";
        }
        
        ret +="<h3>Le Nombre total des heures planifi��es : "
                +getCouleur(nbrHTotalfait,nbrHTotalObligatoire)
                +nbrHTotalfait+"/"
                +nbrHTotalObligatoire+ "</label> Heures</h3>";
        
        return ret;
        }
        else {
            return "key not valide";
        }
    }
    
    
    public static String verifPlaqueHTrvInterv(String key, int annee) {
        if (isKeyValide(key)){
        String ret="";
        
        
        ret +="<H3>Plaquette des heures de travail des intervenants<br/>";
        ret += "Ann��e: "+annee+"/"+(annee+1)+"<br/></H3>";
        
        
        ret += "<table class='calendrier calendrier-etroit'> <caption> <span>Les Intervenants </span></caption>";
        
        ret += "<tr><th>Intervenant</th><th>Nbr H Statutaire/Nbr H Statutaire Autoris��</th><th>Nbr H Sup/Nbr H Sup Autoris��</th><th>D��passement</th><th>Nbr H total effectu��</th><th>Statut</th></tr>";            
        
        TreeMap<String, PlaquetteInterv> map = BDUser.getPlaquetteIterv(annee);
                for (String cle : map.keySet()){
                    
                    ret += "<tr><td>"+cle+"</td>"
                       + "<td>";
                    
                        if(map.get(cle).getNbrHTrv() >= map.get(cle).getNbrHStatutTot()){
                        ret+="<label style='color:#009F3C;'>"
                                +map.get(cle).getNbrHStatutTot()+"/"
                                +map.get(cle).getNbrHStatutTot()+"</label>";
                        }else{
                        
                         ret += getCouleur(map.get(cle).getNbrHTrv() , map.get(cle).getNbrHStatutTot())
                                +map.get(cle).getNbrHTrv()+"/"
                                +map.get(cle).getNbrHStatutTot()+"</label>";
                        }       
                         ret += "</td>";
                   
                         ret += "<td>";
                            
                        if(map.get(cle).getNbrHTrv() <= map.get(cle).getNbrHStatutTot()){
                        ret+="<label style='color:#009F3C;'>0/"
                           +map.get(cle).getNbrHSup()+"</label>";
                        }else{
                        
                         ret += getCouleur((map.get(cle).getNbrHTrv()-map.get(cle).getNbrHStatutTot()) , map.get(cle).getNbrHSup())
                            +(map.get(cle).getNbrHTrv()-map.get(cle).getNbrHStatutTot())+"/"
                            +map.get(cle).getNbrHSup()+"</label>";
                        }  

                         ret += "</td>";
                         
                         if(map.get(cle).getNbrHTrv() <= 
                                 (map.get(cle).getNbrHStatutTot()
                                 +map.get(cle).getNbrHSup())){
                             
                             ret +="<td><label style='color:#009F3C;'>0</label></td>";
                         }
                         else{
                             ret +="<td><label style='color:#F00;'>"
                                 +(map.get(cle).getNbrHTrv()-((map.get(cle).getNbrHStatutTot()
                                 +map.get(cle).getNbrHSup())))
                                 +"</label></td>";
                         }
                         ret += "<td>"+map.get(cle).getNbrHTrv()+"</td>";
                         ret += "<td>"+map.get(cle).getStatut()+"</td>";
                         ret += "</tr>";
                    
                
                }
                
                
        ret += "</table><br/><br/>";
        
        return ret;
        }
        else {
            return "key not valide";
        }
    }
    


    private static String getCouleur(double nbrHCM, double nbrHCMTotal) {
        double pourcentage = (nbrHCM/nbrHCMTotal)*100;
        
        if(pourcentage==100) {
            return "<label style='color:#009F3C;'>";
        }//vert
        else if(pourcentage >=50 && pourcentage <=99) {
            return "<label style='color:#ccaa00;'>";
        }//jaune
            else if(pourcentage >=25 && pourcentage < 50) {
            return "<label style='color:#FA9A00;'>";
        }//orange
        else {
            return "<label style='color:#F00;'>";
        }//rouge
    }

    public static String ActvDesactvCmptInterv(String key, int intervenant, String etat) {
        if (isKeyValide(key)){
         
            
        return EDT.api.BDUser.ActvDesactvCmptInterv(intervenant,etat);
        }
        else {
            return "key not valide";
        }
    }

    public static String modifierCreneau(String key, String[] options, 
            String typeModif, int salle, int interv, String visib, String hdebut, 
            String hfin,int duree, String jour, String timeDecal, String decalage) {
        
        String ret ="";
        
        if (isKeyValide(key)){
            
            
            if(options.length > 0){
            
                ArrayList<Creneau> crs = new ArrayList<>();
                crs = BDUser.getInfoCreneau(options);
                
               /* for (Creneau c : crs) {
                    if(c.getDate().before(new Date())) {
                        return "Erreur vous ne pouvez pas modifier un cr��neau qui a d��j�� ��t�� effectu�� !";
                    }
                }*/
                
                
                int i =0;
                int erreur =0;
                
                DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
                
                ret += BDUser.SupprimerCr(options);        
                
                        switch (typeModif)
                        {
                        case "salle":
                                    
                             while (i< crs.size() ){
                                   Creneau c = crs.get(i);
                                   ret += BDUser.ajouterUnCreneau(c.getId(),
                                        salle,c.getPromotion() ,
                                        c.getIdEc(), c.getIdEns(),
                                        dateFormat.format(c.getDate()),
                                        c.getnGp(), c.getType(),
                                        c.getHdebut(), c.getHfin(),
                                        "true",c.getVisible());
                                   i++;
                                        
                                   if(!ret.equals("")){
                                        erreur++;    
                                   } 
                              }

                            break;
                        
                        
                        case "interv": 
                            while (i< crs.size() ){
                                   Creneau c = crs.get(i);
                                   ret += BDUser.ajouterUnCreneau(c.getId(),
                                        c.getIdSalle(),c.getPromotion() ,
                                        c.getIdEc(), interv,
                                        dateFormat.format(c.getDate()),
                                        c.getnGp(), c.getType(),
                                        c.getHdebut(), c.getHfin(),
                                        "true",c.getVisible());
                                   i++;
                                        
                                   if(!ret.equals("")){
                                        erreur++;    
                                   } 
                              }
                            
                            
                            break;
                        case "visib": 
                            while (i< crs.size()){
                                   Creneau c = crs.get(i);
                                   ret += BDUser.ajouterUnCreneau(c.getId(),
                                        c.getIdSalle(),c.getPromotion() ,
                                        c.getIdEc(), c.getIdEns(),
                                        dateFormat.format(c.getDate()),
                                        c.getnGp(), c.getType(),
                                        c.getHdebut(), c.getHfin(),
                                        "true",visib);
                                   i++;
                                        
                                   if(!ret.equals("")){
                                        erreur++;    
                                   } 
                              }
                            
                            break;
                        case "hdebut": 
                            while (i< crs.size() ){
                                   Creneau c = crs.get(i);
                                   ret += BDUser.ajouterUnCreneau(c.getId(),
                                        c.getIdSalle(),c.getPromotion() ,
                                        c.getIdEc(), c.getIdEns(),
                                        dateFormat.format(c.getDate()),
                                        c.getnGp(), c.getType(),
                                        Integer.parseInt(hdebut.replace(":", "")),
                                        c.getHfin(),
                                        "true",c.getVisible());
                                   i++;
                                        
                                   if(!ret.equals("")){
                                        erreur++;    
                                   } 
                              }
                            
                            
                            break;
                        case "hfin": 
                            while (i< crs.size() ){
                                   Creneau c = crs.get(i);
                                   ret += BDUser.ajouterUnCreneau(c.getId(),
                                        c.getIdSalle(),c.getPromotion() ,
                                        c.getIdEc(), c.getIdEns(),
                                        dateFormat.format(c.getDate()),
                                        c.getnGp(), c.getType(),
                                        c.getHdebut(),
                                        Integer.parseInt(hfin.replace(":", "")),
                                        "true",c.getVisible());
                                   i++;
                                        
                                   if(!ret.equals("")){
                                        erreur++;    
                                   } 
                              }
                            
                            
                            break;
                        case "duree": 
                             while (i< crs.size() ){
                                   Creneau c = crs.get(i);
                                   int hfintmp = c.getHdebut()+((duree/60)*100)+(duree % 60);
                                   ret += BDUser.ajouterUnCreneau(c.getId(),
                                        c.getIdSalle(),c.getPromotion() ,
                                        c.getIdEc(), c.getIdEns(),
                                        dateFormat.format(c.getDate()),
                                        c.getnGp(), c.getType(),
                                        c.getHdebut(),
                                        hfintmp,
                                        "true",c.getVisible());
                                   i++;
                                        
                                   if(!ret.equals("")){
                                        erreur++;    
                                   } 
                              }
                            
                            break;
                        case "jour": 
                            while (i< crs.size()){
                                   Creneau c = crs.get(i);
                                   ret += BDUser.ajouterUnCreneau(c.getId(),
                                        c.getIdSalle(),c.getPromotion() ,
                                        c.getIdEc(), c.getIdEns(),
                                        jour,
                                        c.getnGp(), c.getType(),
                                        c.getHdebut(),
                                        c.getHfin(),
                                        "true",c.getVisible());
                                   i++;
                                        
                                   if(!ret.equals("")){
                                        erreur++;    
                                   } 
                              }
                            
                            break;
                        case "timeDecal": 
                          while (i< crs.size() ){
                                   Creneau c = crs.get(i);
                                   int hdebuttmp=((c.getHdebut()/100)*60)+(c.getHdebut()%100);
                                   int hfintmp=((c.getHfin()/100)*60)+(c.getHfin()%100);
                                   int dec = Integer.parseInt(timeDecal.replace(":", ""));
                                   dec = ((dec/100)*60)+(dec%100);
                                   
                                   if(decalage.equals("-")){ 
                                       hdebuttmp = hdebuttmp-dec;
                                       hdebuttmp= ((hdebuttmp/60)*100)+(hdebuttmp%60);
                                       hfintmp = hfintmp-dec;
                                       hfintmp= ((hfintmp/60)*100)+(hfintmp%60);
                                   }else{
                                       hdebuttmp = hdebuttmp+dec;
                                       hdebuttmp= ((hdebuttmp/60)*100)+(hdebuttmp%60);
                                       hfintmp = hfintmp+dec;
                                       hfintmp= ((hfintmp/60)*100)+(hfintmp%60);
                                   }
                                   ret += BDUser.ajouterUnCreneau(c.getId(),
                                        c.getIdSalle(),c.getPromotion() ,
                                        c.getIdEc(), c.getIdEns(),
                                        dateFormat.format(c.getDate()),
                                        c.getnGp(), c.getType(),
                                        hdebuttmp,
                                        hfintmp,
                                        "true",c.getVisible());
                                   i++;
                                        
                                   if(!ret.equals("")){
                                        erreur++;    
                                   } 
                              }
                            
                            break;
                        default: ret += "Le type de modification n'est pas valide !"; 
                                 erreur++;
                                 
                             break;
                        }
                        
                    //si pas d'erreur je valide les cr��neaux ajout��s 
                    if (erreur != 0){
                    //ret += BDUser.valideCrAjout��(crs.get(0).getPromotion(),
                      //       crs.get(0).getnGp());
                    
                       //Supprimer les cr��neau non valid��.
                       ret += BDUser.SupprimerCr(options);
                       //Ajouter les anciens cr��neaux
                       for(Creneau cr : crs) {
                            ret += BDUser.ajouterUnCreneau(cr.getId(),
                                 cr.getIdSalle(),cr.getPromotion() ,
                                 cr.getIdEc(), cr.getIdEns(),
                                 dateFormat.format(cr.getDate()),
                                 cr.getnGp(), cr.getType(),
                                 cr.getHdebut(), cr.getHfin(),
                                 cr.getValide(),cr.getVisible());
                        } 
                    }
                
            }else{
                
                return "Erreur S��lectionnez un Cr��neau �� modifier";
            }
            
        }
        else {
                    return "key not valide";
                }
        
        
        return ret;
    }
    
    public static String getDateOfPeriode(int idp,int periode){
  
         
            
        return EDT.api.BDUser.getDateOfPeriode(idp,periode);
        
        
    }
    
    public static String getNomFormation(int idp){
  
         
            
        return EDT.api.BDUser.getNomFormation(idp);
        
        
    }
    
    public static Session connexion(String email,String pass){
  
         
            
        return EDT.api.BDUser.connexion(email,pass);
        
        
    }
    
    static String generateKey() 
    {
		/*Random r = new Random();
		int i = r.nextInt();
		return Integer.toHexString(i);
                */
                	String str = "";
                        for (int i=0; i<32; i++)
                        {   
                            int r = (int)(Math.random() * lg);
                            str += hexTab[r%15];
                        }	
                return str;
    }

  


    public static void  deconnexion(String key){
        
        BDUser.supprKeyExpire();
        BDUser.deconnect(key);
    }

    public static String consultFormation(int idf) {
            String ret="";
            
            FormationConsult form = BDUser.getFormation(idf);
            if(form != null){
                
                ret +="<H3>Description de la formation : "
                +form.getNom()+"<br/>";
                ret +="Ann��e d'��xpiration : "+form.getAnneeEx()+"<br/>";
                ret += "Les Promotions cr��es : ";
                for(int promo : form.getLesPromos()) {
                    ret += promo+"  ";
                }    
                ret += "<br/>Le nombre de semestre : "+form.getNbrPeriode()+"<br/>";

                ret +="</H3>";
                Double nbrHTotalObligatoire = 0.0;
        
        for (int i=1;i<=form.getNbrPeriode();i++){
        ret += "<table class='calendrier calendrier-etroit'> <caption> <span>Periode : "+i+" </span></caption>";
        
        ret += "<tr><th>UE</th><th>EC</th><th>Nbr H CM</th><th>Nbr H (TD+TP) </th><th>Coefficient</th></tr>";            
        
        TreeMap<String, PlaquetteForm> map = form.getLesUeUc();
                for (String cle : map.keySet()){
                    
                    if(map.get(cle).getNperiode()==i){
                   
                    
                    nbrHTotalObligatoire += map.get(cle).getNbrHCMTotal()
                                             +map.get(cle).getNbrHTDTMETotal();
                    ret += "<tr><td>"+map.get(cle).getUeNom()+"</td>"
                            + "<td>"+map.get(cle).getEcNom()+"</td>"
                            + "<td>"
                            
                                
                              
                                +map.get(cle).getNbrHCMTotal()+"</td>"
                                
                                        
                            + "<td>"
                            
                               
                              
                                +map.get(cle).getNbrHTDTMETotal()+"</td>"
                                
                                
                                
                              
                                + "<td>"+map.get(cle).getCoeff()+"</td>"
                            
                            
                            +"</tr>";
                    
                    
                    }
                }
        
                ret += "</table><br/>";
             }
        
            ret +="<h3>Le Nombre total d'heures de la formation : "
               +nbrHTotalObligatoire+ " Heures</h3>";
                
                
            }else{
                ret += "Probl��me de connexion veillez r��essayer";
            }
            
            return ret;
    
    }
    
    
    public static ArrayList<String> getCalendrierICS(String type,int id, int grpAnneeEc, int periode,int nbrPeriode){
        
        String pr;
        if(periode==0) {
            pr = "all";
        }
        else {
            pr= String.valueOf(periode);
        }
        
        Calendrier c;
        
        switch(type){
            
            case "promo": c = getCalendrierPromo(id, pr, nbrPeriode, grpAnneeEc);break;
            case "interv": c = getCalendrierInterv(id,grpAnneeEc);break;
            case "ec": c = getCalendrierEc(id,grpAnneeEc);break;
            case "salle": c = getCalendrierSalle(id, grpAnneeEc);break;
            default: c = getCalendrierPromo(id, pr, nbrPeriode, grpAnneeEc);break;
        }
        
        
        return c.getIcsForm();
    }

    public static String modifierPassword(String key, int interv, String passold, String pass) {
        if(isKeyValideInterv(key, interv)){
            
            return BDUser.modifierPasswd(interv,passold,pass);
            
        }else{
            
            return "key not valide";
        }
    }

    public static String reinitialisePassword(String key, int interv, String pass) {
        if(isKeyValide(key)){
            
            return BDUser.reinitialisePasswd(interv,pass);
            
        }else{
            
            return "key not valide";
        }
    }


} 